<template>
  <div class="login">
    <div class="box">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item style="text-align:center">
          <img src="@/assets/common/login-logo.png" alt="">
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" prefix-icon="el-icon-user-solid" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" prefix-icon="el-icon-lock" type="password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 按需导入登录封装的组件
// import { sysLogin } from '@/api/login.js'
export default {
// 表单验证
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const res = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (res.test(value)) {
                callback()
              } else {
                callback(new Error('错误'))
              }
            },
            trigger: 'change'
          }],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 12, message: '密码长度在6到12位之间' }]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          // const res = await sysLogin(this.form)
          // console.log(res)
          // this.$message.success('成功')
          await this.$store.dispatch('user/toLogin', this.form)
          // this.$router.push('/')
          const _req = this.$route.query.req || '/'
          this.$router.push(_req)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login{
  height: 100%;
  background: url('../../assets/common/login.jpg')no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  .box{
    width: 450px;
  }
}
</style>

